﻿using System;
using System.Collections.Generic;

namespace TinyEngine.Core.Services
{
    public sealed class ServiceManager 
    {
        private static Dictionary<Type, ITinyService> services;
        static ServiceManager()
        {
            services = new Dictionary<Type, ITinyService>();
        }
        
        public static void RegisterService(ITinyService service)
        {
            service.Register();
            services.Add(service.GetType(), service);
        }

        public static void UnRegisterService<T>()
        {
            var type = typeof(T);
            if (services.ContainsKey(type))
            {
                var service = services[type];
                service.UnRegister();
                services.Remove(type);
            }
        }

        public static T GetService<T>() where T : class
        {
            var service = services[typeof(T)];
            return service as T;
        }
    }
}
